<template>
  <div class="login">
    <el-form ref="loginRef" :model="state.loginForm" :rules="loginRules" class="login-form">
      <div class="title-box">
        <div class="title-top">
          <img src="@/assets/logo/logo.png" alt="" />
          <h3 class="title">{{ $t('message.sysTitle') }}</h3>
        </div>
        <div class="lang-box" @click="handleChangeLanguage">
          <svg-icon icon-class="langen" class="lang-icon" v-if="lang === 'en'" />
          <svg-icon icon-class="langch" class="lang-icon" v-if="lang === 'zh-cn'" />
        </div>
      </div>
      <el-form-item prop="account">
        <el-input v-model="state.loginForm.account" type="text" size="large" auto-complete="off" :placeholder="$t('message.formI18nLabel.account')">
          <template #prefix>
            <svg-icon icon-class="user" class="el-input__icon input-icon" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="pwd">
        <el-input v-model="state.loginForm.pwd" type="password" size="large" auto-complete="off" :placeholder="$t('message.formI18nLabel.pwd')" @keydown.enter="handleLogin">
          <template #prefix>
            <svg-icon icon-class="password" class="el-input__icon input-icon" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="state.loginForm.code"
          size="large"
          auto-complete="off"
          :placeholder="$t('message.formI18nLabel.regCode')"
          style="width: 63%"
          @keydown.enter="handleLogin"
        >
          <template #prefix>
            <svg-icon icon-class="validCode" class="el-input__icon input-icon" />
          </template>
        </el-input>
        <div class="login-code" v-loading="state.regCodeLoading">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button :loading="loading" size="large" type="primary" style="width: 100%" @click="handleLogin">
          <span v-if="!loading">{{ $t('message.formI18nBtns.login') }}</span>
          <span v-else>{{ $t('message.formI18nBtns.logining') }}</span>
        </el-button>
        <!-- <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div> -->
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2023 samool All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup lang="ts">
import {getCodeImg} from '@apis/login'
import useUserStore from '@store/modules/user'
import useAppStore from '@store/modules/app'
import {onMounted, reactive} from 'vue'
import {getUUID} from '@utils/uuid'
import i18n from '@/i18n'

const userStore = useUserStore()
const appStore = useAppStore()
const router = useRouter()
const loginRef = ref(null) as any
const {t} = i18n.global

const state = reactive({
  regCodeLoading: false,
  loginForm: {
    account: '',
    pwd: '',
    code: '',
    uuid: ''
  }
}) as any

const loginRules = {
  account: [{required: true, trigger: 'blur', message: t('message.formI18nPlaceholder.account')}],
  pwd: [{required: true, trigger: 'blur', message: t('message.formI18nPlaceholder.pwd')}],
  code: [{required: true, trigger: 'change', message: t('message.formI18nPlaceholder.regCode')}]
}

const lang = computed(() => appStore.language)

const handleChangeLanguage = () => {
  appStore.setLanguage(lang.value === 'en' ? 'zh-cn' : 'en')
}

const codeUrl = ref('')
const loading = ref(false)
// 验证码开关
const captchaEnabled = ref(true)
// 注册开关
const register = ref(false)
const redirect = ref(undefined)

function handleLogin() {
  loginRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
      // 调用action的登录方法
      userStore
        .login(state.loginForm)
        .then(() => {
          router.push({path: redirect.value || '/'})
        })
        .catch(() => {
          loading.value = false
          // 重新获取验证码
          if (captchaEnabled.value) {
            getCode()
          }
        })
    }
  })
}

async function getCode() {
  state.regCodeLoading = true
  const uuid: string = getUUID()
  state.loginForm.uuid = uuid
  const codeUrlRes = await getCodeImg({uuid})
  codeUrl.value = codeUrlRes
  state.regCodeLoading = false
}

onMounted(() => {
  getCode()
})
</script>

<style lang="scss" scoped>
@import './login.scss';
</style>
